package cn.thread;

import sun.awt.windows.ThemeReader;

import java.util.ArrayList;
import java.util.List;
//存在改的时候存在线程安全问题，大量的数据存在线程安全问题
public class UnsafeTest {
    public static  void main(String args[]){
        Account account=new Account("存款",1000);
     DrawingMoney father=new DrawingMoney(account,500);
     DrawingMoney mather=new DrawingMoney(account,700);
     new Thread(father,"父亲").start();
     new Thread(mather,"母亲").start();
    }
}
class Account{
    String project;
    int account;
    public  Account(String project,int account){
        this.account=account;
        this.project=project;
    }

}
class DrawingMoney implements Runnable{
    Account account;
    int drawingMoney;
    int cash;
    public DrawingMoney(Account account,int drawingMoney){
        this.account=account;
        this.drawingMoney=drawingMoney;


    }

    @Override
    public  void run() {
        if(account.account<=0){
            return;
        }
           synchronized (account){
            if (account.account - drawingMoney < 0) {
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.account -= drawingMoney;
            cash += drawingMoney;
            System.out.println("银行卡剩余金额：" + account.account);
            System.out.println("取了：" + drawingMoney + "钱" + Thread.currentThread().getName() + "兜里有" + cash + "钱");
        }}
}
class AddList{

    public static void main(String args[]){
        List<String> list=new ArrayList<>();
        for(int i=0;i<1000;i++){
            new Thread(()->{
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
                }).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }

}
